//
//  NotificationService.swift
//  ezyform
//
//  Created by Hubert on 2022/6/13.
//  Copyright © 2022 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import Alamofire
class NotificationService: BaseService {
    func setFireBaseToken(_ logoutFlag: Bool = false){
        let fcmToken = logoutFlag ? "" : APPConfig.loginInfo.fcmToken
        let params: Parameters = ["fire_base_token" : fcmToken]
        let alRequest = ALRequest.init(mainURL: APIConfig.getRequestAPIForV2(APIConfig.SetFirebaseTokenAPI), method: .post, headers: RequestConstant.AuthTokenParam, paramDict: params, successBlock: {(obj, _) -> Void in
            self.returnBlock(obj, nil)
        }, errorBlock: {(code, obj) -> Void in
            self.errorBlock?(code, obj)
        })
        alRequest.startRequest()
    }
    func getNotificationListWithoutCache(_ page: String, searchText: String = ""){
        let params: Parameters = [ "fire_base_token" : APPConfig.loginInfo.fcmToken,
                                       "page": page,
                                       "per_page": 100,
                                       "q": searchText]
        let alRequest = ALRequest.init(mainURL: APIConfig.getRequestAPIForV2(APIConfig.NotificationsListAPI), method: .get, headers: RequestConstant.AuthTokenParam, paramDict: params, successBlock: { (result, otherInfo) -> Void in
            self.returnBlock(result,otherInfo)
        }, errorBlock: { (code, obj) -> Void in
            self.errorBlock?(code, obj)
        })
        alRequest.startRequest(cacheFlag: true)
    }
    // 通知消息列表
    func getNotificationList(_ page: String, searchText: String = ""){
        let params: Parameters = [ "fire_base_token" : APPConfig.loginInfo.fcmToken,
                                       "page": page,
                                       "per_page": 10]
        let alRequest = ALRequest.init(mainURL: APIConfig.getRequestAPIForV2(APIConfig.NotificationsListAPI), method: .get, headers: RequestConstant.AuthTokenParam, paramDict: params, successBlock: { (result, otherInfo) -> Void in
            self.returnBlock(result,otherInfo)
        }, errorBlock: { (code, obj) -> Void in
            self.errorBlock?(code, obj)
        })
        alRequest.startRequest(cacheFlag: true)
    }
    
    // 通知消息详情
    func getNotificationDetail(_ id: String){
        let params: Parameters = ["fire_base_token" : APPConfig.loginInfo.fcmToken,
                                       "id": id]
        let alRequest = ALRequest.init(mainURL: APIConfig.getRequestAPIForV2(APIConfig.NotificationsDetailAPI), method: .get, headers: RequestConstant.AuthTokenParam, paramDict: params, successBlock: {(obj, _  ) -> Void in
            self.returnBlock(obj,nil)
        }, errorBlock: {(code, obj) -> Void in
            self.errorBlock?(code, obj)
        })
        alRequest.startRequest()
    }
    // 通知消息已读, read_id为空，表示设置所有的消息为已读
    func setNotificationReadStatus(){
        let params: Parameters = ["fire_base_token" : APPConfig.loginInfo.fcmToken]
        
        let alRequest = ALRequest.init(mainURL: APIConfig.getRequestAPIForV2(APIConfig.NotificationsReadAPI), method: .post, headers: RequestConstant.AuthTokenParam, paramDict: params, successBlock: {(obj, _ ) -> Void in
            self.returnBlock(obj,nil)
        }, errorBlock: {(code, obj) -> Void in
            self.errorBlock?(code, obj)
        })
        alRequest.startRequest()
    }
    
    // 通知消息未读条数
    func getNotificationUnReadCount(){
        let params: Parameters = ["fire_base_token" : APPConfig.loginInfo.fcmToken]
        let alRequest = ALRequest.init(mainURL: APIConfig.getRequestAPIForV2(APIConfig.NotificationsUnreadCountAPI), method: .get, headers: RequestConstant.AuthTokenParam, paramDict: params, successBlock: {(obj, _ ) -> Void in
            self.returnBlock(obj,nil)
        }, errorBlock: {(code, obj) -> Void in
            self.errorBlock?(code, obj)
        })
        alRequest.startRequest()
    }
}
